package com.boolib.shopweb.web;

import com.boolib.commons.Const;
import com.boolib.commons.aop.annotation.IsLogin;
import com.boolib.commons.entity.ServerResponse;
import com.boolib.load.mail.MailHelper;

import com.boolib.pojo.ShopUser;
import com.boolib.service.IUserService;
import com.boolib.vaild.UserGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@Slf4j
@RestController
@RequestMapping("/shop")
public class UserController {




    @Autowired
    private MailHelper mailHelper;





    @Autowired
    private IUserService iUserService;

    @RequestMapping(value = "/login")
    public ServerResponse<ShopUser> userLogin(@Validated(value = UserGroup.LoginGroup.class) ShopUser shopUser, HttpSession httpSession){

        ServerResponse<ShopUser> serverResponse = iUserService.ShopUserLoginAuth(shopUser);

        if(serverResponse.isSuccess()){
            httpSession.setAttribute(Const.User.SESSIONAUTH,serverResponse.getData());
            return serverResponse;
        }

        return  serverResponse;

    }



    @RequestMapping(value = "/register")
    public ServerResponse<ShopUser> shopUserRegister(@Validated(value = UserGroup.LoginGroup.class) ShopUser shopUser, HttpSession httpSession){

        ServerResponse<ShopUser> serverResponse = iUserService.ShopUserRegister(shopUser);

        if(serverResponse.isSuccess()){
            httpSession.setAttribute(Const.User.SESSIONAUTH,serverResponse.getData());
            return serverResponse;
        }

        return  serverResponse;

    }




    @RequestMapping(value = "/sendMail")
    public  ServerResponse sendMail(){
        mailHelper.sendHtmlMail("nier0001@163.com","834337625@qq.com","【明日科技】","<h1>您的验证码：<b>6379</b></h1> ");
        return ServerResponse.createBySuccessMessage("已发送");
    }


    @RequestMapping(value = "/forgetPass")
    public  ServerResponse shopUserForgetPass(String username){
        ServerResponse serverResponse = iUserService.ShopUserForgetPassword(username);
        return serverResponse;
    }


    @RequestMapping(value = "/userAlterPassBeforeLogin")
    public  ServerResponse shopUserAlterPassBeforeLogin(Integer id,String newPassword,String code){

        return  iUserService.ShopUserAlterPasswordBeforeLogin(id,newPassword,code);
    }





    @IsLogin
    @RequestMapping(value = "/userAlterPassAfterLogin")
    public  ServerResponse shopUserAlterPasswordAfterLogin(String newPassword,HttpSession session){

        ShopUser shopUser = (ShopUser) session.getAttribute(Const.User.SESSIONAUTH);

        if(shopUser == null)
            return  ServerResponse.createByError(Const.System.Status.UNAUTHENTICATION.getCode(),Const.System.Status.UNAUTHENTICATION.getMessage());


        return  iUserService.ShopUserAlterPasswordAfterLogin(shopUser.getId(),newPassword);
    }


    @IsLogin
    @RequestMapping(value = "/userLogout")
    public  ServerResponse shopUserLogout(HttpSession session){
        session.removeAttribute(Const.User.SESSIONAUTH);
        return  iUserService.ShopUserLogout(session);
    }


    @IsLogin
    @RequestMapping(value = "/shopUserAlterAvatar")
    public  ServerResponse shopUserAlterAvatar(String avatar,HttpSession session){

        ShopUser shopUser = getShopUserBySession(session);
        return  iUserService.ShopUserAlterAvatar(shopUser.getId(),avatar);
    }


    @IsLogin
    @RequestMapping(value = "/shopUserAlterInfo")
    public  ServerResponse shopUserAlterInfo(ShopUser shopUser,HttpSession session){
        ShopUser session1 = getShopUserBySession(session);
        shopUser.setId(session1.getId());
        return  iUserService.ShopUserAlterInfo(shopUser);
    }




    @IsLogin
    @RequestMapping(value = "/logout")
    public  ServerResponse shopUserAlterInfo(HttpSession session){
        session.removeAttribute(Const.User.SESSIONAUTH);


        return   iUserService.logout(session);
    }




    private ShopUser getShopUserBySession(HttpSession session){

        return  (ShopUser)session.getAttribute(Const.User.SESSIONAUTH);

    }











}
